﻿using Comm100.Framework.Domain.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Bot.Domain.Entities;
using Bot.Domain.Interfaces;
using Bot.Domain.Bo;
using Castle.Windsor;
using Bot.Domain.Specifications;

namespace Bot.Domain.Categories.Service
{
    public class DialogflowDomainService : IDialogflowDomainService
    {
        private IWindsorContainer _container;
        private readonly IRepository<Guid, Dialogflow> _repository;

        public DialogflowDomainService(IRepository<Guid, Dialogflow> repository, IWindsorContainer container)
        {
            this._repository = repository;
            this._container = container;
        }

        public Dialogflow Create(Dialogflow category)
        {
            return _repository.Create(category);
        }

        public Dialogflow Get(Guid id)
        {
            return _repository.Get(id);
        }

        public Dialogflow Update(DialogflowUpdateBo bo)
        {
            Dialogflow dialogflow = _repository.Get(bo.Id);
            dialogflow.Id = bo.Id;
            dialogflow.SiteId = bo.SiteId;
            dialogflow.BotId = bo.BotId;
            dialogflow.TimeZone = bo.TimeZone;
            dialogflow.IsEnableLogging = bo.IsEnableLogging;
            dialogflow.MatchMode = bo.MatchMode;
            dialogflow.ClassificationThreshold = bo.ClassificationThreshold;
            dialogflow.ProjectAuthJson = bo.ProjectAuthJson;
            dialogflow.SupportedLanguageCodes = bo.SupportedLanguageCodes;
            dialogflow.DisplayName = bo.DisplayName;
            dialogflow.DefaultLanguageCode = bo.DefaultLanguageCode;
            dialogflow.ProjectId = bo.ProjectId;
            dialogflow.IsTestAccount = bo.IsTestAccount;

            return dialogflow;

        }

        public IEnumerable<Dialogflow> List()
        {
            return _repository.ListAll();
        }

        public void Delete(Guid id)
        {
            Dialogflow dialogflow = _repository.Get(id);

            _repository.Delete(dialogflow);

        }

        public Dialogflow GetBySiteIdAndBotId(int siteId, Guid botId)
        {
            return _repository.List(new DialogflowFilterSpecification(siteId, botId)).FirstOrDefault();
        }

        public Dialogflow Random(bool isTest)
        {
            var dialogflows = _repository.List(new DialogflowFilterSpecification(isTest)).ToList();
            return Utils.GetRandomItemFromList<Dialogflow>(dialogflows);
        }
        public bool CheckInUse(string projectId)
        {
            var dialogflow = _repository.List(new DialogflowFilterSpecification(projectId)).FirstOrDefault();
            if(dialogflow!=null&& dialogflow.SiteId <= 0)
            {
                return false;
            }

            return true;
        }
        public IEnumerable<Dialogflow> GetByIsTestAccount(bool isTestAccount)
        {
            return _repository.List(new DialogflowFilterSpecification(isTestAccount));
        }
    }
}
